from car import car
from basicRoute import basicRoute
import time
import random
import config

class route(basicRoute):

    @staticmethod
    def fromBasicRoute(basicRoute,dictionaryEdges):
        result = route(basicRoute.ID)
        result.listOfEdges = []
        for basicEdge in basicRoute.listOfEdges:
            result.listOfEdges.append( dictionaryEdges[basicEdge] )
        
        result.routeMu = basicRoute.routeMu
        result.routeSigma = basicRoute.routeSigma
            
        return result        
    
        
    def __init__(self,ID):
        basicRoute.__init__(self,ID)
        self.time = time.time()
        self.eps = random.gauss( config.routeStartMu , config.routeStartSigma)
        self.wjechalo = 0
        self.wyjechalo = 0
        self.sumaWjechanych = 0.
        while self.eps < 0:
            self.eps = random.gauss( config.routeStartMu , config.routeStartSigma)
           

    def getEdge(self,node):
        for elem in self.listOfEdges:
            if elem.endNode == node:
                return elem
        assert False

    def getPairOfEdges(self,node):
        answer = []
        for elem in self.listOfEdges:
            if len(answer) == 1:
                answer.append(elem)
                return answer
            if elem.endNode == node:
                answer = [elem]
        
        assert len(answer) == 1
        return [answer[0], answer[0]] 

    def getStartNode(self):
        return self.listOfEdges[0].startNode
    
    """
    def __init__(self,ID):
        basicRoute.__init__(self,ID)
        self.time = time.time()
        self.eps = random.gauss( self.routeStartMu , self.routeStartSigma)
        while self.eps < 0:
            self.eps = random.gauss( self.routeStartMu , self.routeStartSigma)    
    """
          
    def creteCar(self):
        if len(self.listOfEdges) > 0:
            startNode = self.getStartNode()
            if time.time() - self.time >= self.eps:
                self.time = time.time()
                self.eps = random.gauss( self.routeMu , self.routeSigma )
                while self.eps < 0:
                    self.eps = random.gauss( self.routeMu , self.routeSigma )
                startNode.addRouteToQueue(self)
            
    def restart(self,time):
        self.creteCar()
        self.eps -= (time.time() - self.time )
        self.time = time

